package de.xXephis.MeineMensa;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MeineMensaActivity extends Activity implements OnSharedPreferenceChangeListener{
	
	DataUpdater updater;
	static final int DATE_DIALOG_ID = 0;
	private static int year=0;
	private static int month=0;
	private static int day=0;
	private Context context;
	ProgressDialog dialog;
	SharedPreferences prefs;
	private boolean isPaused=false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //setContentView(R.layout.main);
        setContentView(R.layout.counter_list);
        
        context=this;
        
        //bind preferences
        PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
        prefs=PreferenceManager.getDefaultSharedPreferences(context);
        prefs.registerOnSharedPreferenceChangeListener(this);
        
        //init dialog
        dialog = new ProgressDialog(this); 
        dialog.setCancelable(false);
        dialog.setMessage("Bitte Warten...");
        dialog.setTitle("Lade");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        
        try {
        	if(savedInstanceState != null && savedInstanceState.containsKey("year") && savedInstanceState.containsKey("month") && savedInstanceState.containsKey("day")) {
        		MeineMensaActivity.year=savedInstanceState.getInt("year");
        		MeineMensaActivity.month=savedInstanceState.getInt("month");
        		MeineMensaActivity.day=savedInstanceState.getInt("day");
        	}
        	else {
        		Calendar today = Calendar.getInstance();
            	
        		MeineMensaActivity.year   =today.get(Calendar.YEAR);
        		MeineMensaActivity.month  =today.get(Calendar.MONTH)+1;
        		MeineMensaActivity.day    =today.get(Calendar.DAY_OF_MONTH);
        	}
        	
        	String year =String.valueOf(MeineMensaActivity.year);
        	String month=String.valueOf(MeineMensaActivity.month);
        	String day  =String.valueOf(MeineMensaActivity.day);
        	String mensa=prefs.getString("mensa", "16");
        	
        	Log.d("day..","XMLDayPOJO d=x.getDay("+day+", "+month+", "+year+", "+mensa+");");
        	
        	//init updater
	        ListView lv=(ListView)this.findViewById(R.id.counter_list_listholder);
	        TextView heading=(TextView)this.findViewById(R.id.counter_list_heading);
	        TextView heading2=(TextView)this.findViewById(R.id.counter_list_heading2);
	        updater=new DataUpdater(this,lv,heading,heading2,dialog);
	        dialog.show();
	        updater.requestUpdate(day, month, year, mensa);
	        
	        //onclicklistener
	        Button updateButton=(Button)this.findViewById(R.id.counter_list_datepickerbutton);
	        updateButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					DatePickerDialog dp=new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
						
						public void onDateSet(DatePicker view, int year, int monthOfYear,
								int dayOfMonth) {
							monthOfYear++;
							dialog.show();
							String mensa=prefs.getString("mensa", "16");
							updater.requestUpdate(String.valueOf(dayOfMonth), String.valueOf(monthOfYear), String.valueOf(year), mensa);
							MeineMensaActivity.year=year;
							MeineMensaActivity.month=monthOfYear;
							MeineMensaActivity.day=dayOfMonth;
						}
					}, MeineMensaActivity.year, MeineMensaActivity.month-1, MeineMensaActivity.day);
					dp.show();
				}
			});
	        
	        Button nextButton=(Button)this.findViewById(R.id.counter_list_next);
	        nextButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					GregorianCalendar cal=new GregorianCalendar();
					cal.set(MeineMensaActivity.year,
							MeineMensaActivity.month-1,
							MeineMensaActivity.day);
					cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
					MeineMensaActivity.year=cal.get(GregorianCalendar.YEAR);
					MeineMensaActivity.month=cal.get(GregorianCalendar.MONTH)+1;
					MeineMensaActivity.day=cal.get(GregorianCalendar.DAY_OF_MONTH);
					dialog.show();
					updater.requestUpdate(
							String.valueOf(MeineMensaActivity.day),
							String.valueOf(MeineMensaActivity.month),
							String.valueOf(MeineMensaActivity.year)
							, prefs.getString("mensa", "16"));
				}
			});

	        Button prevButton=(Button)this.findViewById(R.id.counter_list_prev);
	        prevButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					GregorianCalendar cal=new GregorianCalendar();
					cal.set(MeineMensaActivity.year,
							MeineMensaActivity.month-1,
							MeineMensaActivity.day);
					cal.add(GregorianCalendar.DAY_OF_MONTH, -1);
					MeineMensaActivity.year=cal.get(GregorianCalendar.YEAR);
					MeineMensaActivity.month=cal.get(GregorianCalendar.MONTH)+1;
					MeineMensaActivity.day=cal.get(GregorianCalendar.DAY_OF_MONTH);
					dialog.show();
					updater.requestUpdate(
							String.valueOf(MeineMensaActivity.day),
							String.valueOf(MeineMensaActivity.month),
							String.valueOf(MeineMensaActivity.year)
							, prefs.getString("mensa", "16"));
				}
			});
	        
	        
	        //gesturelistener
	        LinearLayout lowestlayout = (LinearLayout)this.findViewById(R.id.counter_list_lowestCounterLayout);
	        lowestlayout.setOnTouchListener(new ActivitySwipeDetector(this));
	        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
    
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		reloadActual();
	}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	outState.putInt("year", MeineMensaActivity.year);
    	outState.putInt("month", MeineMensaActivity.month);
    	outState.putInt("day", MeineMensaActivity.day);
    }
    
    protected void refreshActual() {
    	String year =String.valueOf(MeineMensaActivity.year);
    	String month=String.valueOf(MeineMensaActivity.month);
    	String day  =String.valueOf(MeineMensaActivity.day);
    	if(isPaused == false)
    		dialog.show();
    	updater.forceUpdate(day, month, year, prefs.getString("mensa", "16"));
	}
    
    protected void reloadActual() {
    	String year =String.valueOf(MeineMensaActivity.year);
    	String month=String.valueOf(MeineMensaActivity.month);
    	String day  =String.valueOf(MeineMensaActivity.day);
    	if(isPaused == false)
    		dialog.show();
    	updater.forceUpdate(day, month, year, prefs.getString("mensa", "16"));
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId())
    	{
    		case R.id.menu_refresh:
    			refreshActual();
    			return true;
    		
    		case R.id.menu_today:
    			GregorianCalendar cal=new GregorianCalendar();
    			String day=String.valueOf(cal.get(GregorianCalendar.DAY_OF_MONTH));
    			String month=String.valueOf(cal.get(GregorianCalendar.MONTH)+1);
    			String year=String.valueOf(cal.get(GregorianCalendar.YEAR));
    			dialog.show();
    			updater.requestUpdate(day, month, year, prefs.getString("mensa", "16"));
    			return true;
    		
    		case R.id.menu_options:
    			Intent optinsLauncher=new Intent(getBaseContext(), Preferences.class);
    			startActivity(optinsLauncher);
    			return true;
    		
    		case R.id.menu_exit:
    			this.finish();
    			return true;
    	}
		return false;
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	isPaused=true;
    }

	@Override
    public void onResume()
    {
    	super.onResume();
    	isPaused=false;
    }
    
}